package com.lovo.controller;

import com.lovo.model.DTO.StudentDTO;
import com.lovo.result.ResponseResult;
import com.lovo.service.StudentService;
import com.lovo.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


//@Controller：标识类为控制器
//@RestController：增强版的@Controller。相当于@Controller和@ResponseBody的结合。
//@ResponseBody：把JSON或XML或普通文本写入HttpServletOutStream
@RestController
@ResponseResult
@RequestMapping("/students")
public class StudentController {
    @Resource
    private StudentService studentService;
//    @RequestMapping(value = "/index",method = RequestMethod.GET)
//    public String index(){
//        return "hello index";
//    }

//在表现层，展示的数据不能用DO来展示，因为在实现时，session查询出结果后就消失了，
// 要得出结果最好封装在DTO里面，DTO不受影响

    @GetMapping("/findAll")
//    相当于@RequestMapping(method = RequestMethod.GET)
    public Object findAll(Integer page, Integer size) {
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 5;
        }
        Object object = studentService.findAll(page, size);
        System.out.println(object);
        return object;
    }

    @GetMapping("/{id}")
    public Object findById(@PathVariable("id") Integer id) {
        return studentService.findByStudentId(id);
    }

    //    这里写dto内容
//    @Valid进行数据验证
    @PostMapping("/add")
    public Object save(@Valid @RequestBody StudentDTO studentDTO) {

        studentService.addStudentBean(studentDTO);

        return null;
    }

    //    修改不支持x-www格式的对象上传，因此
//    @RequestBody 将x-www格式的文件以application/json格式显示
    @PutMapping("/{id}")
    public Object update(@PathVariable("id") Integer id, @RequestBody StudentDTO studentDTO) {
        studentService.update(id, studentDTO);
        return null;
    }

    @DeleteMapping("/{id}")
    public Object delete(@PathVariable("id") Integer id) {
        studentService.del(id);
        return null;
    }


}
